package com.gzy.cbrsystem.core;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;

import com.gzy.cbrsystem.core.insomnia.InsomniaCaseDescription;
import com.gzy.cbrsystem.core.interfaces.SimilarityCalculation;
import com.gzy.cbrsystem.core.interfaces.VectorSimilarity;

public class VectorCosine implements VectorSimilarity{

	public double calculate(InsomniaCaseDescription ia,
			InsomniaCaseDescription ib, List<Field> atts,
			Map<Field, Double> weights, SimilarityCalculation attSim) {
		double abm=0;
		double am=0;
		double bm=0;
		for(Field att:atts)
		{
			try {
				double a=att.getBoolean(ia)?1:0;
				double b=att.getBoolean(ib)?1:0;
				double w=weights.get(att);
				abm+=a*b*w;
				am+=a*a*w;
				bm+=b*b*w;
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		}
		am=Math.sqrt(am);
		bm=Math.sqrt(bm);
		abm=abm/am*bm;
		return abm;
	}

}
